import random
import time
import traceback

from selenium.webdriver.chrome.webdriver import WebDriver

from constants.global_variable import logger
from utils.faker_utils import FakerUtils
from utils.selenium_utils import SeleniumUtils
from utils.web_driver_utils import WebDriverUtils


class CultivatingAccountsTask:
    def __init__(self, web_driver):
        self.web_driver: WebDriver = web_driver

    def run(self):
        try:
            logger.info('开始执行养号任务')
            # 编辑主页，设置姓名和个性签名
            self.edit_profile()
            # 随机关注用户
            self.random_follow_users()
            # 刷视频
            self.scroll_videos()
        except Exception as e:
            logger.error(f'执行养号任务出错，错误信息为{e}')
            stack_trace = traceback.format_exc()
            logger.error(stack_trace)

    def edit_profile(self):
        my_img = WebDriverUtils.find_element_by_name(self.web_driver, '//img[contains(@alt, "的头像")]')
        if my_img:
            self.web_driver.execute_script("arguments[0].click();", my_img)
            time.sleep(2)
        else:
            logger.error('未找到主页我的头像按钮')
        # 点击编译主页
        register_btn = WebDriverUtils.find_elements_with_text(self.web_driver, 'a', '编辑主页')
        if register_btn:
            self.web_driver.execute_script("arguments[0].click();", register_btn)
            time.sleep(2)
        else:
            logger.error('未找到编辑主页按钮')
        # 设置个性签名
        signature_input = WebDriverUtils.find_element_by_id(self.web_driver, 'pepBio')
        if signature_input:
            self.web_driver.execute_script("arguments[0].click();", signature_input)
            time.sleep(1.2)
            signature = FakerUtils.get_fake_bs()
            logger.info(f'生成的个性签名为：{signature}')
            SeleniumUtils.human_typing(signature_input, signature)
            time.sleep(1.4)
        else:
            logger.error('未找到个性签名输入框')
        # 提交
        submit_btn = WebDriverUtils.find_elements_with_text(self.web_driver, 'button', '提交')
        if submit_btn:
            self.web_driver.execute_script("arguments[0].click();", submit_btn)
            time.sleep(5)
        else:
            logger.error('未找到提交按钮')
        # 返回主页
        back_svg = WebDriverUtils.find_element_by_css(self.web_driver, 'svg[aria-label="返回"]')
        if back_svg:
            self.web_driver.execute_script("arguments[0].click();", back_svg)
            time.sleep(2)
        else:
            logger.error('未找到返回按钮')

        

    def random_follow_users(self):
        pass

    def scroll_videos(self):
        logger.info("开始随机刷视频")
        video_svg = WebDriverUtils.find_element_by_css(self.web_driver, 'svg[aria-label="Reels"]')
        if video_svg:
            self.web_driver.execute_script("arguments[0].click();", video_svg)
            time.sleep(random.randint(3, 8))
        else:
            logger.error('未找到视频按钮')
        # 滚动视频
        scroll_times = random.randint(1, 3)
        for i in range(scroll_times):
            SeleniumUtils.scroll(self.web_driver)
